<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, java.util.*" %>
<%@ page import="com.mysql.cj.jdbc.Driver" %> <!-- 导入数据库驱动 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索结果</title>
    <link rel="stylesheet" href="../css/search.css">
</head>
<body>
    <header>
        <div class="header-container">
            <h1 class="logo">学校论坛</h1>
            <nav class="navbar">
                <ul>
                    <li><a href="#">首页</a></li>
                    <li><a href="jsp/studyExchange.jsp">学习交流</a></li>
                    <li><a href="jsp/campusLife.jsp">校园生活</a></li>
                    <li><a href="jsp/clubLife.jsp">社团活动</a></li>
                    <li><a href="jsp/post.jsp">发表帖子</a></li>
                    <li><a href="jsp/myPosts.jsp">我的消息</a></li>
                </ul>
            </nav>
            <a href="jsp/login.jsp" class="login-link">登录</a>
        </div>
    </header>

    <main>
        <h2>搜索结果</h2>
        <%
            String query = request.getParameter("query");
            if (query != null && !query.isEmpty()) {
                try {
                    // 确保加载数据库驱动
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    
                    // 数据库连接信息
                    String DB_URL = "jdbc:mysql://localhost:3306/lczmysql?serverTimezone=UTC";
                    String DB_USER = "root";
                    String DB_PASSWORD = "123456";
                    
                    // 创建数据库连接
                    Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
                    
                    String sql = "SELECT * FROM post WHERE title LIKE ? OR content LIKE ?";
                    PreparedStatement statement = connection.prepareStatement(sql);
                    statement.setString(1, "%" + query + "%");
                    statement.setString(2, "%" + query + "%");
                    ResultSet resultSet = statement.executeQuery();
                    
                    while (resultSet.next()) {
        %>
                    <article class="post">
                        <h2><%= resultSet.getString("title") %></h2>
                        <p class="author">发布者: <%= resultSet.getString("author") %>  发布时间: <%= resultSet.getString("post_time") %> </p>
                        <!-- 获取 content 内容，替换路径 -->
                        <p class="excerpt">
                            <%
                                String content = resultSet.getString("content");
                                // 替换 content 中的图片路径，确保它带有 /luntan 前缀
                                if (content != null) {
                                    content = content.replaceAll("src=\"/ueditor", "src=\"/luntan/ueditor");
                                }
                            %>
                            <%= content %>
                        </p>
                    </article>
                <%
                    }
                    connection.close();  // 关闭数据库连接
                } catch (Exception e) {
                    e.printStackTrace();  // 打印异常信息，便于调试
                }
            }
        %>
    </main>
</body>
</html>
